home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / bin / binutils-config < prev    next >
Text File  |  2006-04-25  |  10KB  |  381 lines

  1. #!/bin/bash
  2. # Copyright 1999-2005 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # $Header: /var/cvsroot/gentoo-x86/sys-devel/binutils-config/files/binutils-config-1.8,v 1.15 2006/04/21 23:51:07 vapier Exp $
  5.  
  6. # Format of /etc/env.d/binutils/:
  7. #  config-TARGET:    CURRENT=version for TARGET
  8. #  TARGET-VER:        has a TARGET and VER variable
  9.  
  10. cd /
  11.  
  12. trap ":" INT QUIT TSTP
  13.  
  14. argv0=${0##*/}
  15. source /etc/init.d/functions.sh || {
  16.     echo "${argv0}: Could not source /etc/init.d/functions.sh!" 1>&2
  17.     exit 1
  18. }
  19. esyslog() { :; }
  20. umask 022
  21.  
  22. usage() {
  23. cat << USAGE_END
  24.  
  25. Usage: ${HILITE}binutils-config${NORMAL} ${GOOD}[options]${NORMAL} ${BRACKET}[binutils profile]${NORMAL}
  26.  
  27. ${HILITE}General Options:${NORMAL}
  28.   ${GOOD}-c, --get-current-profile${NORMAL}  Print current profile
  29.   ${GOOD}-l, --list-profiles${NORMAL}        Print a list of available profiles
  30.   ${GOOD}-u, --uninstall${NORMAL}            Remove all signs of specified target
  31.   ${GOOD}-d, --debug${NORMAL}                Execute with debug output
  32.  
  33. ${HILITE}Arch Specific Cruft:${NORMAL}
  34.   ${GOOD}--amd64${NORMAL}                    Install extra amd64 links (x86_64)
  35.   ${GOOD}--arm${NORMAL}                      Install extra arm links (arm/armeb)
  36.   ${GOOD}--mips${NORMAL}                     Install extra mips links (mips/mipsel)
  37.   ${GOOD}--x86${NORMAL}                      Install extra x86 links (i[3-6]86)
  38.  
  39. Profile names are of the form:  ${BRACKET}<CTARGET>-<binutils version>${NORMAL}
  40. For example:                    ${BRACKET}i686-pc-linux-gnu-2.15.92.0.2${NORMAL}
  41.  
  42. For more info, please see ${HILITE}binutils-config${NORMAL}(8).
  43. USAGE_END
  44. }
  45.  
  46. switch_profile() {
  47.     unset TARGET VER LIBPATH FAKE_TARGETS
  48.     source "${ENV_D}/${PROFILE}"
  49.     if [[ -z ${TARGET} ]] ; then
  50.         eerror "${PROFILE} is invalid (no \$TARGET defined) :("
  51.         return 1
  52.     fi
  53.     if [[ -z ${VER} ]] ; then
  54.         eerror "${PROFILE} is invalid (no \$VER defined) :("
  55.         return 1
  56.     fi
  57.  
  58.     #
  59.     # Older installs don't have 'FAKE_TARGETS' defined, so lets 
  60.     # update these env.d entries so that we don't force the poor 
  61.     # user to re-emerge their binutils just for 1 envvar :/
  62.     #
  63.     if [[ ${FAKE_TARGETS-poor user} == "poor user" ]] ; then
  64.         local targ=${TARGET/-*}
  65.         local FAKE_TARGETS=${TARGET}
  66.         case ${targ} in
  67.             mips|powerpc|sparc)
  68.                 FAKE_TARGETS="${FAKE_TARGETS} ${TARGET/-/64-}";;
  69.             mips64|powerpc64|sparc64)
  70.                 FAKE_TARGETS="${FAKE_TARGETS} ${TARGET/64-/-}";;
  71.         esac
  72.         echo "FAKE_TARGETS=\"${FAKE_TARGETS}\"" >> "${ENV_D}/${PROFILE}"
  73.     fi
  74.     local fake_targ_append="${TARGET#*-}"
  75.     FAKE_TARGETS="${FAKE_TARGETS} ${FAKE_TARGETS_USER// /-${fake_targ_append} }"
  76.  
  77.     ebegin "Switching to ${PROFILE}"
  78.  
  79.     #
  80.     # Generate binary symlinks
  81.     # On systems that do 32bit/64bit, we need to fake an 
  82.     # extra set of binary names (${FAKE_TARGETS})
  83.     #
  84.     BINPATH=/usr/${TARGET}/binutils-bin/${VER}
  85.     cd "${ROOT}/${BINPATH}" || exit 1
  86.     mkdir -p "${ROOT}"/usr/${TARGET}/bin
  87.     for x in * ; do
  88.         ln -sf "${BINPATH}/${x}" "${ROOT}"/usr/${TARGET}/bin/${x}
  89.         ln -sf ../${TARGET}/bin/${x} "${ROOT}"/usr/bin/${TARGET}-${x}
  90.         for fake in ${FAKE_TARGETS} ; do
  91.             [[ -f ${ENV_D}/config-${fake} ]] && continue
  92.             ln -sf ../${TARGET}/bin/${x} "${ROOT}"/usr/bin/${fake}-${x}
  93.         done
  94.         if [[ ${TARGET} == ${HOST} ]] ; then
  95.             ln -sf ${TARGET}-${x} "${ROOT}"/usr/bin/${x}
  96.         fi
  97.     done
  98.  
  99.     #
  100.     # Generate library / ldscripts symlinks
  101.     #
  102.     LIBPATH=${LIBPATH:-/usr/lib/binutils/${TARGET}/${VER}}
  103.     cd "${ROOT}/${LIBPATH}" || exit 1
  104.     mkdir -p "${ROOT}"/usr/${TARGET}/lib
  105.     if [[ -d ${ROOT}/usr/${TARGET}/lib/ldscripts ]] ; then
  106.         # When upgrading, we need to clean up ldscripts
  107.         rm -r "${ROOT}"/usr/${TARGET}/lib/ldscripts
  108.     else
  109.         rm -f "${ROOT}"/usr/${TARGET}/lib/ldscripts
  110.     fi
  111.     ln -sf "${LIBPATH}/ldscripts" "${ROOT}"/usr/${TARGET}/lib/ldscripts
  112.     if [[ ${TARGET} == ${HOST} ]] ; then
  113.         dstlib=${ROOT}/usr/${HOST}/lib
  114.     else
  115.         dstlib=${ROOT}/usr/${HOST}/${TARGET}/lib
  116.     fi
  117.     mkdir -p "${dstlib}"
  118.     for x in lib* ; do
  119.         ln -sf "${LIBPATH}/${x}" "${dstlib}/${x}"
  120.     done
  121.  
  122.     #
  123.     # Generate include symlinks
  124.     #
  125.     INCPATH=${LIBPATH}/include
  126.     if [[ -d ${ROOT}/${INCPATH} ]] ; then
  127.         cd "${ROOT}/${INCPATH}" || exit 1
  128.         if [[ ${TARGET} == ${HOST} ]] ; then
  129.             dstinc=${ROOT}/usr/include
  130.         else
  131.             dstinc=${ROOT}/usr/${TARGET}/usr/include
  132.         fi
  133.         mkdir -p "${dstinc}"
  134.         for x in * ; do
  135.             ln -sf "${INCPATH}/${x}" "${dstinc}/${x}"
  136.         done
  137.     fi
  138.  
  139.     #
  140.     # Make sure proper paths get updated
  141.     #
  142.     if [[ ${TARGET} == ${HOST} ]] ; then
  143.         DATAPATH=/usr/share/binutils-data/${TARGET}/${VER}
  144.         [[ -d ${DATAPATH}/man ]] && \
  145.         echo "MANPATH=${DATAPATH}/man" > "${ROOT}"/etc/env.d/05binutils
  146.         [[ -d ${DATAPATH}/info ]] && \
  147.         echo "INFOPATH=${DATAPATH}/info" >> "${ROOT}"/etc/env.d/05binutils
  148.         echo "LDPATH=/usr/${TARGET}/lib" >> "${ROOT}"/etc/env.d/05binutils
  149.     fi
  150.  
  151.     eend 0
  152.  
  153.     echo "CURRENT=${VER}" > "${ENV_D}/config-${TARGET}"
  154.  
  155.     #
  156.     # Regen env.d if need/can be
  157.     #
  158.     if [[ ${ROOT} == "/" ]] && [[ ${TARGET} == ${HOST} ]] ; then
  159.         env-update
  160.         echo
  161.         ewarn "Please remember to run:"
  162.         echo
  163.         ewarn "  # source /etc/profile"
  164.         echo
  165.     fi
  166.  
  167.     return 0
  168. }
  169.  
  170. uninstall_target() {
  171.     if [[ ${TARGET} == ${HOST} ]] ; then
  172.         eerror "${argv0}: Refusing to uninstall native binutils"
  173.         exit 1
  174.     fi
  175.  
  176.     shopt -s nullglob
  177.     PROFILE=""
  178.  
  179.     for PROFILE in "${ENV_D}"/${TARGET}-* ; do
  180.         ewarn "Removing all signs of ${PROFILE##*/}"
  181.         rm -f "${ENV_D}"/${PROFILE}
  182.     done
  183.     if [[ -z ${PROFILE} ]] && [[ ! -e ${ENV_D}/config-${TARGET} ]] ; then
  184.         eerror "${argv0}: No profiles exist for '${TARGET}'"
  185.         exit 1
  186.     fi
  187.  
  188.     rm -f "${ENV_D}"/config-${TARGET}
  189.  
  190.     # XXX: we still leave behind FAKE_TARGETS in /usr/bin ...
  191.     for x in addr2line ar as c++filt elf2flt flthdr gprof ld ld.real \
  192.              nm objcopy objdump ranlib readelf size strings strip ; do
  193.         rm -f "${ROOT}"/usr/bin/${TARGET}-${x}
  194.         rm -f "${ROOT}"/usr/${TARGET}/bin/${x}
  195.     done
  196.     for x in ansidecl.h bfd.h bfdlink.h dis-asm.h symcat.h ; do
  197.         rm -f "${ROOT}"/usr/${TARGET}/{usr/,}include/${x}
  198.     done
  199.     for x in bfd iberty opcodes ; do
  200.         rm -f "${ROOT}"/usr/${HOST}/${TARGET}/lib/lib${x}{{-*,}.so,.a,.la}
  201.     done
  202.     # Delete broken symlinks
  203.     find "${ROOT}"/usr/${TARGET}/lib -xtype l -exec rm {} \;
  204.     rmdir "${ROOT}"/usr/${HOST}/${TARGET}/lib "${ROOT}"/usr/${HOST}/${TARGET} 2>/dev/null
  205.     rm -f "${ROOT}"/usr/${TARGET}/lib/ldscripts
  206.     rmdir "${ROOT}"/usr/${TARGET}/{usr/,}{bin,include,lib,usr} "${ROOT}"/usr/${TARGET} 2>/dev/null
  207.     rmdir "${ROOT}"/var/db/pkg/cross-${TARGET} 2>/dev/null
  208.  
  209.     rm -f "${ENV_D}"/${TARGET}-*
  210. }
  211.  
  212. get_current_profile() {
  213.     if [[ ! -f ${ENV_D}/config-${PROFILE} ]] ; then
  214.         eerror "${argv0}: No binutils profile is active!"
  215.         return 1
  216.     fi
  217.  
  218.     source "${ENV_D}/config-${PROFILE}"
  219.  
  220.     if [[ -z ${CURRENT} ]] ; then
  221.         eerror "${argv0}: No binutils profile is active!"
  222.         return 1
  223.     fi
  224.  
  225.     echo "${PROFILE}-${CURRENT}"
  226.  
  227.     return 0
  228. }
  229.  
  230. list_profiles() {
  231.     local i=1
  232.  
  233.     if [[ ${ROOT} != / ]] ; then
  234.         echo "Using binutils-config info in ${ROOT}"
  235.     fi
  236.     target=
  237.     for x in "${ENV_D}"/* ; do
  238.         if [[ -f ${x} ]] && [[ ${x/\/config-} == ${x} ]] ; then
  239.             source "${x}"
  240.             if [[ ${target} != ${TARGET} ]] ; then
  241.                 [[ -n ${target} ]] && echo
  242.                 target=${TARGET}
  243.             fi
  244.  
  245.             x=${x##*/}
  246.             if [[ -e ${ENV_D}/config-${TARGET} ]] ; then
  247.                 source "${ENV_D}/config-${TARGET}"
  248.                 [[ ${VER} == ${CURRENT} ]] && x="${x} ${GOOD}*${NORMAL}"
  249.             fi
  250.             echo " [${i}] ${x}"
  251.             ((++i))
  252.         fi
  253.     done
  254. }
  255.  
  256. set_HOST() {
  257.     [[ -n ${HOST} ]] && return 0
  258.  
  259.     if [[ -z ${CHOST} ]] ; then
  260.         HOST=$(portageq envvar CHOST)
  261.     else
  262.         HOST=${CHOST}
  263.     fi
  264. }
  265.  
  266. [[ -z ${ROOT} ]] && ROOT="/"
  267. [[ ${ROOT:0-1} != "/" ]] && ROOT="${ROOT}/"
  268. ENV_D="${ROOT}etc/env.d/binutils"
  269.  
  270. DEBUG="no"
  271. NEED_ACTION="yes"
  272. DOIT="switch_profile"
  273. PROFILE=""
  274. FAKE_TARGETS_USER=""
  275. HOST=""
  276.  
  277. while [[ $# -gt 0 ]] ; do
  278.     x=$1
  279.     shift
  280.     case ${x} in
  281.         -c|--get-current-profile)
  282.             if [[ ${NEED_ACTION} == "yes" ]] ; then
  283.                 NEED_ACTION="no"
  284.                 DOIT="get_current_profile"
  285.             fi
  286.             ;;
  287.         -d|--debug)
  288.             DEBUG="yes"
  289.             ;;
  290.         -l|--list-profiles)
  291.             if [[ ${NEED_ACTION} == "yes" ]] ; then
  292.                 NEED_ACTION="no"
  293.                 DOIT="list_profiles"
  294.             fi
  295.             ;;
  296.         -u|--uninstall)
  297.             if [[ ${NEED_ACTION} == "yes" ]] ; then
  298.                 NEED_ACTION="no"
  299.                 DOIT="uninstall_target"
  300.                 TARGET=$1
  301.                 shift
  302.             fi
  303.             ;;
  304.         -h|--help)
  305.             usage
  306.             exit 0
  307.             ;;
  308.         -V|--version)
  309.             cvsver="$Header: /var/cvsroot/gentoo-x86/sys-devel/binutils-config/files/binutils-config-1.8,v 1.15 2006/04/21 23:51:07 vapier Exp $"
  310.             cvsver=${cvsver##*binutils-config-}
  311.             echo "binutils-config-${cvsver%%,v *}"
  312.             exit 0
  313.             ;;
  314.         --amd64|--arm|--mips|--x86)
  315.             if [[ ${NEED_ACTION} == "yes" ]] ; then
  316.                 # Make sure we have a space after each target
  317.                 NEED_ACTION="no"
  318.                 [[ -z ${PROFILE} ]] && PROFILE="current"
  319.                 case ${x} in
  320.                     --amd64) FAKE_TARGETS_USER="x86_64 ";;
  321.                     --arm)   FAKE_TARGETS_USER="arm armeb ";;
  322.                     --x86)   FAKE_TARGETS_USER="i386 i486 i586 i686 ";;
  323.                     --mips)  FAKE_TARGETS_USER="mips mips64 mipsel mipsel64 ";;
  324.                 esac
  325.             fi
  326.             ;;
  327.         -*)
  328.             eerror "${0##*/}: Invalid switch!  Try '--help'."
  329.             exit 1
  330.             ;;
  331.         *)
  332.             if [[ -n ${PROFILE} ]] && [[ ${PROFILE} != "current" ]] ; then
  333.                 eerror "${argv0}: Too many arguments!  Run ${argv0} without parameters for help."
  334.                 exit 1
  335.             fi
  336.  
  337.             if [[ -z $(echo ${x} | tr -d '[:digit:]') ]] ; then
  338.                 # User gave us a # representing the profile
  339.                 i=1
  340.                 for y in "${ENV_D}"/* ; do
  341.                     [[ ${y/config-} != ${y} ]] && continue
  342.  
  343.                     if [[ -f ${y} ]] && [[ ${x} -eq ${i} ]] ; then
  344.                         PROFILE=${y##*/}
  345.                         NEED_ACTION="no"
  346.                         break
  347.                     fi
  348.                     ((++i))
  349.                 done
  350.             fi
  351.  
  352.             if [[ -z ${PROFILE} ]] ; then
  353.                 # User gave us a full HOST-ver
  354.                 x=${x##*/}
  355.                 if [[ ! -f ${ENV_D}/${x} ]] && [[ ! -f ${ENV_D}/config-${x} ]] ; then
  356.                     # Maybe they just gave us a ver ...
  357.                     set_HOST
  358.                     if [[ -f ${ENV_D}/${HOST}-${x} ]] ; then
  359.                         x=${HOST}-${x}
  360.                     else
  361.                         eerror "${argv0}: Could not locate '$x' in '${ENV_D}/'!"
  362.                         exit 1
  363.                     fi
  364.                 fi
  365.                 PROFILE=${x}
  366.                 NEED_ACTION="no"
  367.             fi
  368.             ;;
  369.     esac
  370. done
  371.  
  372. [[ ${NEED_ACTION} == "yes" ]] && usage && exit 1
  373. [[ ${DEBUG} == "yes" ]] && set -x
  374.  
  375. [[ ${DOIT} != "list_profiles" ]] && set_HOST
  376. [[ -z ${PROFILE} ]] && PROFILE=${HOST}
  377. [[ ${PROFILE} == "current" ]] && PROFILE=$(PROFILE=${HOST} get_current_profile)
  378. eval ${DOIT}
  379.  
  380. # vim:ts=4
  381.